﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// xinxi_save 的摘要说明
    /// </summary>
    public class xinxi_save : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "qiugou_add": //提交求购
                    qiugou_add(context);
                    break;
                case "gongying_add": //提交供应
                    gongying_add(context);
                    break;
                case "get_gongying_category": //获取供求产品分类
                    get_gongying_category(context);
                    break;
                case "job_add": //提交求职招聘
                    job_add(context);
                    break;
                case "wuliu_add": //提交求职招聘
                    wuliu_add(context);
                    break;
                case "get_zhoubian_category": //提交求职招聘
                    get_zhoubian_category(context);
                    break;
                case "zhoubian_add": //提交周边信息
                    zhoubian_add(context);
                    break;
                case "guestbook_add": //提交留言信息
                    guestbook_add(context);
                    break;
                case "xunjia_add": //提交询价信息
                    xunjia_add(context);
                    break;
            }
        }

        #region 提交求购信息===========================
        private void qiugou_add(HttpContext context)
        {
       
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            BLL.qiugou bll = new BLL.qiugou();
            Model.qiugou model = new Model.qiugou();

            string _title = DTRequest.GetFormString("txtTitle");
            int _long = DTRequest.GetFormInt("txtLong", 0);
            int _wide = DTRequest.GetFormInt("txtWide", 0);
            int _height = DTRequest.GetFormInt("txtHeight", 0);
            string _remark = DTRequest.GetFormString("txtRemark");
            int _quanzi_id = DTRequest.GetFormInt("txtQuanziID");

            if (string.IsNullOrEmpty(_title))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入标题！\"}");
                return;
            }
            if (_long == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入长度！\"}");
                return;
            }
            if (_wide == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入宽度！\"}");
                return;
            }
            if (_height == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入高度！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }

            model.title = _title;
            model.size_long = _long;
            model.size_wide = _wide;
            model.size_height = _height;
            model.content = _remark;
            model.add_time = DateTime.Now;
            model.status = 1;
            model.user_id = userModel.id;
            model.lianxiren = userModel.user_name;
            model.mobile = userModel.mobile;
            model.quanzi_id = _quanzi_id;

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.common_albums> ls = new List<Model.common_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    string thumb_path = FileSave(context, myFile, true);//20190415 水印
                    string filePath = thumb_path.Replace("thumb_", "");
                    ls.Add(new Model.common_albums { channel_id = 2, original_path = filePath, thumb_path = thumb_path });
                    //设置默认图片
                    if (i == 0)
                    {
                        model.img_url = thumb_path;
                    }
                }
                model.albums = ls;
            }
            #endregion
           
            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 提交供应信息===========================
        private void gongying_add(HttpContext context)
        {

            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            //判断当前用户是否有权限
            //Model.user_groups groupModel = new BLL.user_groups().GetModel(userModel.group_id);
            //if (groupModel == null)
            //{
            //    context.Response.Write("{\"status\":0, \"msg\":\"出错，无法获取用户组信息！\"}");
            //    return;
            //}
            //int total_count = new BLL.gongying().GetCount("user_id=" + userModel.id + " and datediff(day,add_time,getdate())=0");
            //if (total_count >= groupModel.gongqiu_add)
            //{
            //    context.Response.Write("{\"status\":0, \"msg\":\"抱歉，您每天只能发布"+groupModel.gongqiu_add+"条内容，您已超标！\"}");
            //    return;
            //}

            BLL.gongying bll = new BLL.gongying();
            Model.gongying model = new Model.gongying();

            string _title = DTRequest.GetFormString("txtTitle");
            int _category_id = DTRequest.GetFormInt("slCategoryID", 0);
            int _price = DTRequest.GetFormInt("txtPrice", 0);
            string _unit = DTRequest.GetFormString("slUnit");
            int _long = DTRequest.GetFormInt("txtLong", 0);
            int _wide = DTRequest.GetFormInt("txtWide", 0);
            int _height = DTRequest.GetFormInt("txtHeight", 0);
            string _remark = DTRequest.GetFormString("txtRemark");
            int _quanzi_id = DTRequest.GetFormInt("txtQuanziID");

            if (string.IsNullOrEmpty(_title))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入标题！\"}");
                return;
            }
            if (_long == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入长度！\"}");
                return;
            }
            if (_wide == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入宽度！\"}");
                return;
            }
            if (_height == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入高度！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }
            model.category_id = _category_id;
            model.title = _title;
            model.price = _price;
            model.unit = _unit;
            model.size_long = _long;
            model.size_wide = _wide;
            model.size_height = _height;
            model.content = _remark;
            model.add_time = DateTime.Now;
            model.status = 1;
            model.user_id = userModel.id;
            model.lianxiren = userModel.user_name;
            model.mobile = userModel.mobile;
            model.quanzi_id = _quanzi_id;

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.common_albums> ls = new List<Model.common_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    string thumb_path = FileSave(context, myFile, true);//20190415 水印
                    string filePath = thumb_path.Replace("thumb_", "");
                    ls.Add(new Model.common_albums { channel_id = 1, original_path = filePath, thumb_path = thumb_path });
                    //设置默认图片
                    if (i == 0)
                    {
                        model.img_url = thumb_path;
                    }
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 获取产品分类===========================
        private void get_gongying_category(HttpContext context)
        {
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.gongying_category bll = new BLL.gongying_category();
            DataSet ds = bll.GetList(0, "", "sort_id asc,id asc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"category_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }
        #endregion

        #region 提交求职招聘信息===========================
        private void job_add(HttpContext context)
        {

            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            BLL.jobs bll = new BLL.jobs();
            Model.jobs model = new Model.jobs();

            string _title = DTRequest.GetFormString("txtTitle");
            int _type_id = DTRequest.GetFormInt("slTypeID", 0);
            int _category_id = DTRequest.GetFormInt("slCategoryID", 0);
            string _remark = DTRequest.GetFormString("txtRemark");
            int _quanzi_id = DTRequest.GetFormInt("txtQuanziID");

            if (string.IsNullOrEmpty(_title))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入标题！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }
            model.title = _title;
            model.type_id = _type_id;
            model.category_id = _category_id;
            model.province_id = 0;
            model.city_id = 0;
            model.district_id = 0;
            model.user_id = userModel.id;
            model.company_id = 0;
            model.content = _remark;
            model.add_time = DateTime.Now;
            model.status = 1;
            model.quanzi_id = 0;
            model.lianxiren = userModel.user_name;
            model.mobile = userModel.mobile;
            model.quanzi_id = _quanzi_id;

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 提交物流信息===========================
        private void wuliu_add(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            int _quanzi_id = DTRequest.GetFormInt("txtQuanziID");

            BLL.wuliu bll = new BLL.wuliu();
            Model.wuliu model = new Model.wuliu();

            int _type_id = DTRequest.GetFormInt("slTypeID", 0);
            string _start_address = DTRequest.GetFormString("txtStartAddress");
            string _end_address = DTRequest.GetFormString("txtEndAddress");
            string _remark = DTRequest.GetFormString("txtRemark");

            if (string.IsNullOrEmpty(_start_address) || string.IsNullOrEmpty(_end_address))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入始发地和目的地！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }

            model.type_id = _type_id;
            model.start_address = _start_address;
            model.end_address = _end_address;
            model.remark = _remark;
            model.status = 1;
            model.hits = 0;
            model.add_time = DateTime.Now;
            model.user_id = userModel.id;
            model.quanzi_id = 0;
            model.lianxiren = userModel.user_name;
            model.mobile = userModel.mobile;
            model.quanzi_id = _quanzi_id;

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 获取周边服务分类===========================
        private void get_zhoubian_category(HttpContext context)
        {
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.zhoubian_category bll = new BLL.zhoubian_category();
            DataSet ds = bll.GetList(0, "", "sort_id asc,id asc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"category_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }
        #endregion

        #region 提交周边信息===========================
        private void zhoubian_add(HttpContext context)
        {

            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            BLL.zhoubian bll = new BLL.zhoubian();
            Model.zhoubian model = new Model.zhoubian();

            string _title = DTRequest.GetFormString("txtTitle");
            int _category_id = DTRequest.GetFormInt("slCategoryID", 0);
            string _remark = DTRequest.GetFormString("txtRemark");
            int _quanzi_id = DTRequest.GetFormInt("txtQuanziID");
          
            if (string.IsNullOrEmpty(_title))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入标题！\"}");
                return;
            }
           
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }

            model.category_id = _category_id;
            model.title = _title;
            model.remark = _remark;
            model.user_id = userModel.id;
            model.quanzi_id = 0;
            model.add_time = DateTime.Now;
            model.hits = 0;
            model.status = 1;
            model.lianxiren = userModel.user_name;
            model.mobile = userModel.mobile;
            model.quanzi_id = _quanzi_id;

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.common_albums> ls = new List<Model.common_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    string thumb_path = FileSave(context, myFile, true);//20190415 水印
                    string filePath = thumb_path.Replace("thumb_", "");
                    ls.Add(new Model.common_albums { channel_id = 4, original_path = filePath, thumb_path = thumb_path });
                    //设置默认图片
                    if (i == 0)
                    {
                        model.img_url = thumb_path;
                    }
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 提交留言信息===========================
        private void guestbook_add(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            BLL.guestbook bll = new BLL.guestbook();
            Model.guestbook model = new Model.guestbook();

            string _realname = DTRequest.GetFormString("txtRealName");
            string _mobile = DTRequest.GetFormString("txtMobile");
            string _remark = DTRequest.GetFormString("txtRemark");

            if (string.IsNullOrEmpty(_realname))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入您的真实姓名！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_mobile))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入您的手机号！\"}");
                return;
            }

            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }

            model.realname = _realname;
            model.mobile = _mobile;
            model.remark = _remark;
            model.user_id = userModel.id;
            model.add_time = DateTime.Now;
            model.status = 0;

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.common_albums> ls = new List<Model.common_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    string thumb_path = FileSave(context, myFile, true);//20190415 水印
                    string filePath = thumb_path.Replace("thumb_", "");
                    ls.Add(new Model.common_albums { channel_id = 5, original_path = filePath, thumb_path = thumb_path });
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功，请等待管理员审核！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        #region 提交询价信息===========================
        private void xunjia_add(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            BLL.xunjia bll = new BLL.xunjia();
            Model.xunjia model = new Model.xunjia();

            string _remark = DTRequest.GetFormString("txtRemark");
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }

            model.remark = _remark;
            model.add_time = DateTime.Now;
            model.status = 0;
            model.user_id = userModel.id;

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.xunjia_albums> ls = new List<Model.xunjia_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    string thumb_path = FileSave(context, myFile, true);//20190415 水印
                    string filePath = thumb_path.Replace("thumb_", "");
                    ls.Add(new Model.xunjia_albums { original_path = filePath, thumb_path = thumb_path });
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        /// <summary>
        /// 统一保存文件
        /// </summary>
        private string FileSave(HttpContext context, HttpPostedFile upFiles, bool isWater)
        {
            //获取文件信息
            string fileName = upFiles.FileName;
            byte[] byteData = FileHelper.ConvertStreamToByteBuffer(upFiles.InputStream); //获取文件流
            //开始上传
            string remsg = new UpLoad().FileSaveAs(byteData, fileName, true, isWater);
            Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(remsg);
            string status = dic["status"].ToString();
            string msg = dic["msg"].ToString();
            if (status == "0")
            {
                //showError(context, msg);
                return "";
            }
            string filePath = dic["thumb"].ToString(); //取得上传后的路径
            return filePath;
            //showSuccess(context, fileName, filePath); //输出成功提示
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}